package com.qx.grainmall.elasticsearch.web;

import com.qx.common.vo.SkuEsModel;
import com.qx.grainmall.elasticsearch.service.EsService;
import com.qx.grainmall.elasticsearch.vo.SearchParam;
import com.qx.grainmall.elasticsearch.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
public class WebSearchController {
    @Autowired
    private EsService esService;
    @RequestMapping(value = {"/", "search.html"})
    public String toseearch(SearchParam searchParam, //检索的参数
                            Model model, HttpServletRequest request) throws IOException {
        searchParam.set_queryString(request.getQueryString()); //所有查询属性
        SearchResult result = esService.getSearchResult(searchParam);
        model.addAttribute("result",result);
        return "search";
    }
    //批量上架
    @PostMapping("up")
    @ResponseBody
    public Boolean up(@RequestBody List<SkuEsModel> list) throws IOException {
        return esService.addBulk(list);
    }

}
